package com.SH.controller;

import com.SH.Utils.Utils;
import com.SH.pojo.Category;
import com.SH.pojo.Goods;
import com.SH.pojo.User;
import com.SH.service.CategoryService;
import com.SH.service.GoodsService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.ResourceUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * @Description:
 * @Project:
 * @CreateDate: Created in 2020/3/4 15:06
 * @Author: YXH
 */
@Controller
public class GoodsController {

    @Autowired
    private GoodsService goodService;

    @Autowired
    private CategoryService categoryService;


    @GetMapping("/findGoods")
    public String findGoods(String name, Model model){
        System.out.println(name);
        List<Goods> goods = goodService.findGoods(name);
        model.addAttribute("findGoods",goods);
        return "list";
    }


    @GetMapping("/getGoods")
    public String getGoods(@RequestParam(defaultValue = "1") Integer pageNum, @RequestParam(defaultValue = "5") Integer pageSize, Model model){
        PageHelper.startPage(pageNum,pageSize);
        List<Goods> goods = goodService.getAllGood();

        PageInfo pageInfo = new PageInfo(goods);
        model.addAttribute("goods",pageInfo);
        return "backstage/goods";
    }

    @GetMapping("/deleteGoods/{id}")
    public String deleteGoods(@PathVariable("id") Integer id){
        goodService.delGoods(id);
        return "redirect:/getGoods";
    }

    @GetMapping("/sellingGoods")
    public String sellingGoods(@RequestParam(name = "uid") String uid, Model model){
        int id = Integer.valueOf(uid).intValue();
        System.out.println(id);
        List<Goods> sellingGoods = goodService.sellingGoods(id);
        for (Goods goods:sellingGoods) {
            System.out.println(goods);
        }
        model.addAttribute("sellingGoods",sellingGoods);
        return "goods/sellingGoods";
    }

    @GetMapping("/cancel")
    public String cancel(@RequestParam(name = "id") String id, RedirectAttributes attributes){
        int uid = Integer.valueOf(id).intValue();
        Goods goods = goodService.getById(uid);
        attributes.addAttribute("uid",goods.getUid());
        goodService.cancelGood(goods);
        return "redirect:/sellingGoods";
    }

    @GetMapping("/edit")
    public String edit(@RequestParam(name = "id")String id,Model model){
        int ID = Integer.valueOf(id).intValue();
        Goods goods = goodService.getById(ID);
        System.out.println(goods);
        model.addAttribute("goods",goods);
        return "goods/edit";
    }


    @GetMapping("/publish")
    public String addGoods(Model model){
        List<Category> categoryList = categoryService.allCategory();
        System.out.println(categoryList);
        model.addAttribute("categoryList",categoryList);
        return "goods/publish";
    }

    @PostMapping("/editGoods")
    public String editGoods(Goods goods,RedirectAttributes attributes){
        Goods goods1 = goodService.getById(goods.getId());
        goods1.setPrice(goods.getPrice());
        goods1.setName(goods.getName());
        goods1.setGdesc(goods.getGdesc());
        System.out.println(goods1);
        goodService.update(goods1);
        attributes.addAttribute("uid",goods1.getUid());
        return "redirect:/sellingGoods";
    }

    @PostMapping("/addGoods")
    public String addPhone(RedirectAttributes attributes,HttpSession session,HttpServletRequest request, @RequestParam(value = "file") MultipartFile[] files, String name, Integer cid, double price , String gdesc){
        System.out.println(name+","+cid+","+price+","+","+gdesc);
        System.out.println( files.length);
        try {
            Goods goods = new Goods();
            goods.setName(name);
            goods.setPrice(price);
            goods.setGdesc(gdesc);
           User user = (User) session.getAttribute("user");
           goods.setUid(user.getId());
           goods.setIs_selling(1);
           goods.setCid(cid);


            File path = new File(ResourceUtils.getURL("classpath:").getPath());
            if(!path.exists()){
                path=new File("");
            }
            File upload =new File(path.getAbsolutePath(),"static/images/");
            if(!upload.exists()){
                upload.mkdirs();
            }
            for (MultipartFile file : files) {
               String uuid = Utils.getUUID();
                String originalFilename = file.getOriginalFilename();
                String extendName = originalFilename.substring(originalFilename.lastIndexOf("."), originalFilename.length());
                String fileName = uuid + extendName;
                File dir = new File(upload.getAbsolutePath(), fileName);
                file.transferTo(dir);
                System.out.println("上传成功");
               goods.setImage(fileName);
            }
            System.out.println(goods);
            attributes.addAttribute("uid",goods.getUid());
            System.out.println(upload.getAbsolutePath());
            goodService.addGoods(goods);
        } catch (IOException e) {
            e.printStackTrace();
        }
        //在开发测试模式时，得到地址为：{项目跟目录}/target/static/images/upload/
        //在打成jar正式发布时，得到的地址为:{发布jar包目录}/static/images/upload/
        return "redirect:/sellingGoods";
    }


}
